<?php

namespace app\index\controller;

use app\common\model\AppraiseModel;
use think\Controller;
use think\Db;
use app\api\model\GroupTour;

class Group extends Controller
{
    /**
     * 显示资源列表
     */
    public function groupList()
    {

        $kinds = input('param.kinds');

        $where['is_del'] = 0;
        $where['is_display'] = 0;
        switch ($kinds) {
            case 1:
                $where['gtour_days'] = 1;
                break;
            case 2:
                $where['gtour_days'] = 2;
                break;
        }

        $title = input('param.title');
        if($title){
            $where['gtour_title'] = ['like','%'.$title.'%'];
        }

        //查询商家列表
        $groupList = Db::name('group_tour')
            ->field('id,gtour_title,gtour_covers,gtour_price,gtour_days,sale_sum')
            ->where($where)
            ->order('id desc')
            ->paginate(15,false,['query' => request()->param()]);

        $list = $groupList->items();

        $this->assign('groupList',$groupList);
        $this->assign('list',$list);
        return view();

    }

    /**跟团游详情**/
    public function group($id){

        $group = Db::name('group_tour')->where('id',$id)->find();

        $group['gtour_label'] = explode(',',$group['gtour_label']);

        //获取三张酒店图片
        $groupImg = Db::name('gtour_tour_img')->where('gt_id',$id)->order('sort','desc')->select();

        //好评率
        $Appraise = new AppraiseModel();
        $group['rate'] = $Appraise->getAppRate(3,$id);

        //统计评论条数
        $group['comment_count'] = Db::name('appraise')->where('shop_id',$id)->where('is_display',0)->where('type',3)->count();
        $group['latests_comment'] = Db::name('appraise')->field('content')->where('shop_id',$id)->where('type',3)->order('app_id','desc')->find();

        //获取行程
        $journey = Db::name('gtour_journey')->where('gt_id',$id)->order('journey_id','desc')->select();

        foreach ($journey as $k => $v){

            $journey[$k]['sort_day'] = self::numToWord($v['sort_day']);

        }

        //查询旅行社
        $team = Db::name('team')->field('team_name,team_phone')->where('team_id',$group['team_id'])->find();

<<<<<<< HEAD
        //判断是否数收藏该
=======
        //判断是否数收藏该酒店
>>>>>>> 23a64db8e3abcbc49d76a293e2eb5456ae25d7ad
        $is_collect = 0;

        if (session('uid')) {
            $find_collect = Db::name('collect')->where('type',3)->where('other_id',$id)->find();

            if (!empty($find_collect)) {
                $is_collect = 1;
            }
        }

<<<<<<< HEAD
        //查询班期
        $dateTime = date('Y-m-d');

        $gschedule = Db::name('gtour_gschedule')
                                ->field('tg_date,tg_price')
                                ->where('tg_id','EQ',$id)
                                ->where('tg_date','> time',$dateTime)
                                ->select();

        $gscheduleData = [];
        foreach ($gschedule as $k => $v){

            $gscheduleData[$k]['date'] = $v['tg_date'];
            $gscheduleData[$k]['data'] = $v['tg_price']*1;

        }

        
        $this->assign('gscheduleData',json_encode($gscheduleData));
=======
        /***/
        $groupTour = new gt(); 
        $result = $groupTour->getGscheduleListById($id);

        
>>>>>>> 23a64db8e3abcbc49d76a293e2eb5456ae25d7ad
        $this->assign('is_select',$is_collect);
        $this->assign('group',$group);
        $this->assign('team',$team);
        $this->assign('groupImg',$groupImg);
        $this->assign('journey',$journey);
        return view();

    }

    /**获取酒店评价**/
    public function getComment(){

        if (request()->isAjax()){

            $param = request()->param();

            $comment = Db::name('appraise')
                ->alias('t1')
                ->field('t1.content,t1.images,t1.eval_status,t1.create_time,t2.nickname,t2.user_icon')
                ->join('lv_user t2','t1.user_id = t2.uid','left')
                ->where('shop_id',$param['id'])
                ->where('type',3)
                ->page($param['page'],5)
                ->select();

            foreach ($comment as $k => $v){

                if ($v['images']){
                    $comment[$k]['images'] = explode(',',$v['images']);
                }else{
                    $comment[$k]['images'] = [];
                }

                $comment[$k]['create_time'] = date("Y-m-d H:i",$v['create_time']);

            }

            return ['code'=>0,'data'=>$comment];

        }

    }

    private function numToWord($num)
    {
        $chiNum = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
        $chiUni = array('','十', '百', '千', '万', '亿', '十', '百', '千');

        $chiStr = '';

        $num_str = (string)$num;

        $count = strlen($num_str);
        $last_flag = true; //上一个 是否为0
        $zero_flag = true; //是否第一个
        $temp_num = null; //临时数字

        $chiStr = '';//拼接结果
        if ($count == 2) {//两位数
            $temp_num = $num_str[0];
            $chiStr = $temp_num == 1 ? $chiUni[1] : $chiNum[$temp_num].$chiUni[1];
            $temp_num = $num_str[1];
            $chiStr .= $temp_num == 0 ? '' : $chiNum[$temp_num];
        }else if($count > 2){
            $index = 0;
            for ($i=$count-1; $i >= 0 ; $i--) {
                $temp_num = $num_str[$i];
                if ($temp_num == 0) {
                    if (!$zero_flag && !$last_flag ) {
                        $chiStr = $chiNum[$temp_num]. $chiStr;
                        $last_flag = true;
                    }
                }else{
                    $chiStr = $chiNum[$temp_num].$chiUni[$index%9] .$chiStr;

                    $zero_flag = false;
                    $last_flag = false;
                }
                $index ++;
            }
        }else{
            $chiStr = $chiNum[$num_str[0]];
        }
        return $chiStr;
    }

}